//
//  LYJProgressHUD.m
//  HXDemo
//
//  Created by 李亚军 on 16/11/25.
//  Copyright © 2016年 李亚军. All rights reserved.
//

#import "LYJProgressHUD.h"
#import "ProgressHUDBottomVC.h"
#import "BoardView.h"

@interface LYJProgressHUD ()


@end

static UIWindow *customWindow = nil;
static ProgressHUDBottomVC *progressHUDBottomVC = nil;
static NSTimer *timer = nil;
static NSInteger limitTimerNumber = 0;
static BOOL isAllowedShow = YES;
@implementation LYJProgressHUD



+ (void)showWithStatus:(NSString *)status userInteractionEnabled:(BOOL)userInteractionEnabled animation:(BOOL)animation {

    [LYJProgressHUD initAttributes];
    
    customWindow.userInteractionEnabled = userInteractionEnabled;
    
    isAllowedShow = YES;
    customWindow.hidden = NO;
    progressHUDBottomVC.statusWithActivityView.alpha = 1.0;
    
    if (status == nil || status.length <= 0) {
        
        progressHUDBottomVC.titleLabel.text = @"";
        progressHUDBottomVC.titleMarginToRight.constant = 0;
    }else {
        
        progressHUDBottomVC.titleLabel.text = status;
        progressHUDBottomVC.titleMarginToRight.constant = 10;
    }
    
    limitTimerNumber = 2;
    progressHUDBottomVC.activityIndicatorHeight.constant = 0;
    progressHUDBottomVC.activityIndicatorViewMarginToLeft.constant = 0;
    progressHUDBottomVC.activityIndicatorView.hidden = YES;
    [progressHUDBottomVC.activityIndicatorView stopAnimating];
    
    
    if (animation) {
        
        [UIView animateWithDuration:0.3 delay:1.7 options:(UIViewAnimationOptionCurveLinear) animations:^{
            isAllowedShow = NO;
            progressHUDBottomVC.statusWithActivityView.alpha = 0.0;
        } completion:^(BOOL finished) {
            
            if (finished) {
                
                if (!isAllowedShow) {
                    [LYJProgressHUD dismiss];
                    isAllowedShow = YES;
                }
                
            }
            
        }];
        
    }else {
        
        isAllowedShow = NO;
        [LYJProgressHUD performSelector:@selector(statusWithActivityViewHidden) withObject:nil afterDelay:2];
    }
}


+ (void)showWithStatus:(NSString *)status target:(nullable id)target animation:(BOOL)animation {
    
    
}


+ (void)statusWithActivityViewHidden {
    
    if (!isAllowedShow) {
        [LYJProgressHUD dismiss];
        isAllowedShow = YES;
    }

}


/// 初始化类
+ (void)initAttributes {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"LYJProgressHUD" bundle:nil];
        progressHUDBottomVC = [storyboard instantiateViewControllerWithIdentifier:@"ProgressHUDBottomVCID"];
        
        customWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        customWindow.hidden = NO;
        customWindow.windowLevel = UIWindowLevelAlert + 1;
        customWindow.userInteractionEnabled = NO;
        customWindow.rootViewController = progressHUDBottomVC;
        [customWindow addSubview:progressHUDBottomVC.view];
 
        isAllowedShow = YES;
    });
}


+ (void)dismiss {
    
    customWindow.hidden = YES;
    progressHUDBottomVC.statusWithActivityView.alpha = 1.0;
}


@end
